package com.party.core.service.crowdfund.impl;

import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.party.common.constant.Constant;
import com.party.common.paging.Page;
import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.DateUtils;
import com.party.common.utils.LangUtils;
import com.party.core.dao.read.activity.ActivityReadDao;
import com.party.core.dao.read.crowdfund.*;
import com.party.core.dao.write.crowdfund.ProjectWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.YesNoStatus;
import com.party.core.model.activity.Activity;
import com.party.core.model.crowdfund.*;
import com.party.core.model.member.Member;
import com.party.core.service.crowdfund.IProjectService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.builder.BuilderException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 众筹项目服务接口实现
 * Created by wei.li
 *
 * @date 2017/2/16 0016
 * @time 18:40
 */
@Service
public class ProjectService implements IProjectService, com.party.core.api.crowdfund.IProjectService {

    @Autowired
    private ProjectReadDao projectReadDao;

    @Autowired
    private ProjectWriteDao projectWriteDao;

    @Autowired
    private SupportReadDao supportReadDao;

    @Autowired
    private TargetProjectReadDao targetProjectReadDao;

    @Autowired
    private ActivityReadDao activityReadDao;

    @Autowired
    private EventGradationReadDao eventGradationReadDao;

    @Autowired
    private ProjectGradationReadDao projectGradationReadDao;

    /**
     * 项目插入
     *
     * @param project 项目信息
     * @return 项目编号
     */
    @Override
    public String insert(Project project) {
        BaseModel.preInsert(project);
        boolean result = projectWriteDao.insert(project);
        if (result) {
            return project.getId();
        }
        return null;
    }

    /**
     * 项目更新
     *
     * @param project 项目信息
     * @return 是否更新成功（true/false）
     */
    @Override
    public boolean update(Project project) {
        project.setUpdateDate(new Date());
        return projectWriteDao.update(project);
    }


    @Override
    public int crowdfundRefundFaile(Project project) {
        if (project.getPayType() == Constant.IS_DIRECTPAY_CROWDFUNDED) {
            project.setIsSuccess(5);//设置为退款失败
            project.setRefundState(1);//设置为未退款
            return projectWriteDao.update(project) ? 1 : 0;
        }
        return 0;
    }

    /**
     * 逻辑删除项目
     *
     * @param id 实体主键
     * @return 是否删除成功（true/false）
     */
    @Override
    public boolean deleteLogic(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return projectWriteDao.deleteLogic(id);
    }

    /**
     * 物理删除项目信息
     *
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean delete(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return projectWriteDao.delete(id);
    }

    /**
     * 批量逻辑删除项目信息
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDeleteLogic(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return projectWriteDao.batchDeleteLogic(ids);
    }

    /**
     * 批量物理删除项目信息
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDelete(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return projectWriteDao.batchDelete(ids);
    }

    /**
     * 根据编号获取项目信息
     *
     * @param id 主键
     * @return 项目信息
     */
    @Override
    public Project get(String id) {
        return projectReadDao.get(id);
    }

    /**
     * 获取项目信息包含创建者
     *
     * @param id 编号
     * @return 项目信息
     */
    @Override
    public ProjectWithAuthor getWithAuthor(String id) {
        return projectReadDao.getWithAuthor(id);
    }

    /**
     * 分页查询项目信息
     *
     * @param param 项目信息
     * @param page  分页信息
     * @return 项目列表
     */
    @Override
    public List<Project> listPage(HashMap<String, Object> param, Page page) {
        return projectReadDao.listWithSupportPage(param, page);
    }

    /**
     * 查询所有项目信息
     *
     * @param param 项目信息
     * @return 项目列表
     */
    @Override
    public List<Project> list(HashMap<String, Object> param) {
        return projectReadDao.listWithSupportPage(param, null);
    }

    /**
     * 分页查询项目信息
     *
     * @param project 项目信息
     * @param page    分页信息
     * @return 项目列表
     */
    @Override
    public List<Project> listPage(Project project, Page page) {
        return projectReadDao.listPage(project, page);
    }

    /**
     * 查询所有项目信息
     *
     * @param project 项目信息
     * @return 项目列表
     */
    @Override
    public List<Project> list(Project project) {
        return projectReadDao.listPage(project, null);
    }

    /**
     * 联合创建者查询
     *
     * @param projectWithAuthor 查询参数
     * @param page              分页参数
     * @return 项目列表
     */
    @Override
    public List<ProjectWithAuthor> listPage(ProjectWithAuthor projectWithAuthor, Page page) {
        return projectReadDao.listWithAuthorPage(projectWithAuthor, page);
    }

    /**
     * 联合创建者查询
     *
     * @param projectWithAuthor 查询参数
     * @param page              分页参数
     * @return 项目列表
     */
    @Override
    public List<ProjectWithAuthor> listForDistributorId(ProjectWithAuthor projectWithAuthor, Page page) {
        return projectReadDao.listForDistributorId(projectWithAuthor, page);
    }

    /**
     * 联合创建者查询所有
     *
     * @param projectWithAuthor 查询参数
     * @return 项目列表
     */
    @Override
    public List<ProjectWithAuthor> list(ProjectWithAuthor projectWithAuthor) {
        return projectReadDao.listWithAuthorPage(projectWithAuthor, null);
    }

    /**
     * 批量查询项目信息
     *
     * @param ids     主键集合
     * @param project 项目信息
     * @param page    分页信息
     * @return
     */
    @Override
    public List<Project> batchList(@NotNull Set<String> ids, Project project, Page page) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.EMPTY_LIST;
        }
        return projectReadDao.batchList(ids, new HashedMap(), page);
    }

    /**
     * 是否众筹成功
     *
     * @param targetAmount 目标筹集资金
     * @param actualAmount 实际筹集资金
     * @return 是否成功（true/false）
     */
    public boolean isSuccess(Float targetAmount, Float actualAmount) {
        if (BigDecimalUtils.round(actualAmount, 2) >= BigDecimalUtils.round(targetAmount, 2)) {
            return true;
        }
        return false;
    }

    /**
     * 获取可筹集最大金额
     *
     * @param id 众筹编号
     * @return 最大金额
     */
    @Override
    public Double getMaxAmount(String id) {
        Project project = this.get(id);
        EventGradation ongoingGradation = eventGradationReadDao.getOngoingGradation(id);
        Double num;
        if (ongoingGradation != null) {
            /** 说明这个众筹者  是多场活动*/
            List<EventGradation> eventGradationList = eventGradationReadDao.getAfterEventGradations(ongoingGradation);
            /** 剩余活动总金额*/
            Double targetAmount = ongoingGradation.getPrice();
            for (EventGradation eventGradation : eventGradationList) {
                targetAmount += eventGradation.getPrice();
            }
            /** 已消费活动总金额*/
            Double consumptionAmount = 0.00;
            List<ProjectGradation> successListForProjectId = projectGradationReadDao.getSuccessListForProjectId(id);
            if (successListForProjectId.size() > 0) {
                successListForProjectId.removeAll(Collections.singleton(null));
                for (ProjectGradation projectGradation : successListForProjectId) {
                    consumptionAmount += projectGradation.getTargetAmount();
                }
            }
            /** 累计金额减去已消费活动总金额*/
            num = BigDecimalUtils.sub(project.getActualAmount(), consumptionAmount);
            num = BigDecimalUtils.round(num, 2);
            /** 剩余阶段总金额减去累计金额等于还需要众筹的金额*/
            num = BigDecimalUtils.sub(targetAmount.floatValue(), num);
            num = BigDecimalUtils.round(num, 2);
            num = num < 0 ? 0 : num;
        }else {
            /** 说明这个众筹者  是单场活动*/
            Double realTimeAmount = supportReadDao.sumByProjectId(id, null);
            realTimeAmount = realTimeAmount == null ? 0 : realTimeAmount;
            num = BigDecimalUtils.sub(project.getTargetAmount(), realTimeAmount);
            num = BigDecimalUtils.round(num, 2);
            num = num < 0 ? 0 : num;
        }
        return num;
    }


    /**
     * 根据分销关系查询众筹项目
     *
     * @param distributionId 分销编号
     * @return 项目列表
     */
    @Override
    public List<Project> listForDistributionId(String distributionId) {
        return projectReadDao.listForDistributionId(distributionId);
    }

    /**
     * 根据目标查询众筹项目
     *
     * @param targetId 目标编号
     * @return 项目列表
     */
    @Override
    public List<Project> listForTargetId(String targetId) {
        return projectReadDao.listForTargetId(targetId);
    }

    @Override
    public Double getTotalAmountForTargetId(String targetId) {
        return projectReadDao.getTotalAmountForTargetId(targetId);
    }

    @Override
    public List<Project> listForAuthorId(String authorId, String eventId) {
        return projectReadDao.listForAuthorId(authorId, eventId);
    }

    /**
     * 到期众筹
     *
     * @return 众筹列表
     */
    @Override
    public List<ProjectWithAuthor> endList() {
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("end", true);
        param.put("success", Constant.IS_CROWFUND_ING); //众筹中的
        return projectReadDao.list(param, null);
    }

    /**
     * 众筹中
     *
     * @return 众筹列表
     */
    @Override
    public List<ProjectWithAuthor> underwayList() {
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("success", Constant.IS_CROWFUND_ING); //众筹中的
        param.put("crowdfundHintSwitch", YesNoStatus.YES.getCode());//允许通知
        return projectReadDao.list(param, null);
    }

    /**
     * 众筹成功的
     *
     * @return 众筹列表
     */
    @Override
    public List<ProjectWithAuthor> successList() {
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("success", Constant.IS_CROWFUND_SUCCESS); //众筹成功
        param.put("crowdfundHintSwitch", YesNoStatus.YES.getCode());//允许通知
        return projectReadDao.list(param, null);
    }


    /**
     * 所有众筹
     *
     * @return 众筹列表
     */
    @Override
    public List<ProjectWithAuthor> allList() {
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("crowdfundHintSwitch", YesNoStatus.YES.getCode());//允许通知
        return projectReadDao.list(param, null);
    }

    /**
     * 进行中的
     *
     * @param targetId 目标编号
     * @return 众筹列表
     */
    @Override
    public List<ProjectWithAuthor> underwayList(String targetId) {
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("success", Constant.IS_CROWFUND_ING); //众筹中的
        param.put("targetId", targetId);
        return projectReadDao.list(param, null);
    }

    /**
     * 成功的
     *
     * @param targetId 目标编号
     * @return 众筹列表
     */
    @Override
    public List<ProjectWithAuthor> successList(String targetId) {
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("success", Constant.IS_CROWFUND_SUCCESS); //众筹成功
        param.put("targetId", targetId);
        return projectReadDao.list(param, null);
    }

    /**
     * 事件下众筹中
     *
     * @param eventId 事件编号
     * @return 众筹列表
     */
    @Override
    public List<ProjectWithAuthor> eventUnderwayList(String eventId) {
        if (Strings.isNullOrEmpty(eventId)) {
            throw new BuilderException("事项编号不能为空");
        }
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("success", Constant.IS_CROWFUND_ING); //众筹中的
        param.put("eventId", eventId);
        return projectReadDao.list(param, null);
    }

    /**
     * 事件成功众筹
     *
     * @param eventId 事件编号
     * @return 众筹列表
     */
    @Override
    public List<ProjectWithAuthor> eventSuccessList(String eventId) {
        if (Strings.isNullOrEmpty(eventId)) {
            throw new BuilderException("事项编号不能为空");
        }
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("success", Constant.IS_CROWFUND_SUCCESS); //众筹成功
        param.put("eventId", eventId);
        return projectReadDao.list(param, null);
    }

    /**
     * 事件下所有众筹
     *
     * @param eventId 事件编号
     * @return 众筹列表
     */
    @Override
    public List<ProjectWithAuthor> eventAllList(String eventId) {
        if (Strings.isNullOrEmpty(eventId)) {
            throw new BuilderException("事项编号不能为空");
        }
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("eventId", eventId);
        return projectReadDao.list(param, null);
    }

    /**
     * 所有众筹
     *
     * @param targetId 目标编号
     * @return 众筹列表
     */
    @Override
    public List<ProjectWithAuthor> allList(String targetId) {
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("targetId", targetId);
        return projectReadDao.list(param, null);
    }


    /**
     * 判断众筹是否过期
     *
     * @param project 众筹项目
     * @return 是否过期(true / false)
     */
    @Override
    public boolean isOverdue(Project project) {
        int result = DateUtils.compareDate(new Date(), project.getEndDate());
        if (result == 1) {
            return false;
        }
        return true;
    }

    /**
     * 项目下的众筹数
     *
     * @param targetId 项目编号
     * @return 众筹数
     */
    @Override
    public Integer sizeForTargetId(String targetId) {
        return projectReadDao.sizeForTargetId(targetId, null, null);
    }

    /**
     * 项目下的众筹数
     *
     * @param targetId   项目编号
     * @param relationId 分销关系
     * @param isSuccess  是否成功
     * @return 众筹数
     */
    @Override
    public Integer sizeForTargetId(String targetId, String relationId, Integer isSuccess) {
        return projectReadDao.sizeForTargetId(targetId, isSuccess, relationId);
    }


    /**
     * 项目下的众筹数
     *
     * @param targetId  项目编号
     * @param isSuccess 状态
     * @return 众筹数
     */
    @Override
    public Integer sizeForTargetId(String targetId, Integer isSuccess) {
        Integer size = projectReadDao.sizeForTargetId(targetId, isSuccess, null);
        return null == size ? 0 : size;
    }

    /**
     * 项目下的直接付款众筹数
     *
     * @param targetId
     * @param isSuccess
     * @param payType
     * @return
     */
    @Override
    public Integer directPaySizeForTargetId(String targetId, Integer isSuccess, Integer payType) {
        Integer size = projectReadDao.directPaySizeForTargetId(targetId, isSuccess, payType);
        return null == size ? 0 : size;
    }

    /**
     * 根据事件编号查询众筹数
     *
     * @param eventId 事件编号
     * @return 众筹数
     */
    @Override
    public Integer countForEvent(String eventId) {
        return projectReadDao.countForEvent(eventId, null);
    }

    /**
     * 根据事件编号查询项目
     *
     * @param eventId   事件编号
     * @param isSuccess 状态
     * @return 项目数
     */
    @Override
    public Integer countForEvent(String eventId, Integer isSuccess) {
        Integer size = projectReadDao.countForEvent(eventId, isSuccess);
        return null == size ? 0 : size;
    }

    @Override
    public Integer dpCountForEvent(String eventId, Integer isSuccess) {
        Integer size = projectReadDao.dpCountForEvent(eventId, isSuccess);
        return null == size ? 0 : size;
    }

    /**
     * 众筹分析列表
     *
     * @param projectAnalyze 众筹分析
     * @param page           分页参数
     * @return 众筹分析列表
     */
    @Override
    public List<ProjectAnalyze> analyzeList(ProjectAnalyze projectAnalyze, Page page) {
        List<ProjectAnalyze> list = analyzeList(projectAnalyze, true, page);
        List<String> ids = LangUtils.transform(list, new Function<ProjectAnalyze, String>() {
            @Override
            public String apply(ProjectAnalyze projectAnalyze) {
                return projectAnalyze.getId();
            }
        });
        projectAnalyze.setIds(ids);
        return analyzeList(projectAnalyze, false, null);
    }

    /**
     * 众筹分析列表
     *
     * @param projectAnalyze 众筹分析
     * @param groupBy        是否分组
     * @param page           分页参数
     * @return 众筹分析列表
     */
    @Override
    public List<ProjectAnalyze> analyzeList(ProjectAnalyze projectAnalyze, boolean groupBy, Page page) {
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("authorName", projectAnalyze.getAuthorName());
        param.put("parentName", projectAnalyze.getParentName());
        param.put("isFriend", projectAnalyze.getIsFriend());
        param.put("isGroup", projectAnalyze.getIsGroup());
        param.put("isSuccess", projectAnalyze.getIsSuccess());
        param.put("labelId", projectAnalyze.getLabelId());
        param.put("sort", projectAnalyze.getSort());
        param.put("operator", projectAnalyze.getOperator());
        param.put("operatorNum", projectAnalyze.getOperatorNum());
        param.put("groupBy", groupBy);
        param.put("ids", projectAnalyze.getIds());
        param.put("targetId", projectAnalyze.getTargetId());
        param.put("eventId", projectAnalyze.getEventId());
        param.put("cityName", projectAnalyze.getCityName());
        param.put("company", projectAnalyze.getCompany());
        param.put("mobile", projectAnalyze.getMobile());
        param.put("targetAmount", projectAnalyze.getTargetAmount());
        param.put("payType", projectAnalyze.getPayType());
        param.put("publisher", projectAnalyze.getPublisher());
        param.put("amountGOE", projectAnalyze.getAmountGOE());
        param.put("amountLOE", projectAnalyze.getAmountLOE());
        param.put("currentGradationId", projectAnalyze.getCurrentGradationId());
        return projectReadDao.analyzeList(param, page);
    }

    /**
     * 其他的众筹
     *
     * @param projectId 众筹
     * @return 众筹列表
     */
    @Override
    public List<ProjectWithAuthor> otherProject(String projectId) {
        TargetProject targetProject = targetProjectReadDao.findByProjectId(projectId);
        Activity activity = activityReadDao.get(targetProject.getTargetId());
        ProjectWithAuthor projectWithAuthor = getWithAuthor(projectId);
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("targetAuthorId", activity.getMember());
        param.put("authorId", projectWithAuthor.getAuthorId());
        List<ProjectWithAuthor> list = projectReadDao.list(param, null);

        Integer removeId = null;
        for (int i = 0; i < list.size(); i++) {
            ProjectWithAuthor p = list.get(i);
            if (p.getId().equals(projectWithAuthor.getId())) {
                removeId = i;
            }
        }
        if (null != removeId) {
            list.remove(removeId.intValue());
        }

        return list;
    }


    /**
     * 众筹下支持者总数
     *
     * @param targetId 目标编号
     * @return 支持者总数
     */
    @Override
    public Integer sumfavorerNum(String targetId) {
        return projectReadDao.sumfavorerNum(targetId);
    }

    /**
     * 支持者众筹数
     *
     * @param eventId   事件编号
     * @param targetId  目标编号
     * @param projectId 项目编号
     * @return 众筹数
     */
    @Override
    public Integer numOfSupport(String eventId, String targetId, String projectId) {
        return projectReadDao.numOfSupport(eventId, targetId, projectId);
    }

    /**
     * 根据活动统计众筹
     *
     * @param activityId 活动编号
     * @param status     状态
     * @return 统计数
     */
    @Override
    public Integer countByActivity(String activityId, Integer status) {
        HashMap<String, Object> parameter = Maps.newHashMap();
        parameter.put("activityId", activityId);
        parameter.put("isSuccess", status);
        return projectReadDao.count(parameter);
    }

    /**
     * 根事项统计众筹
     *
     * @param eventId 事项编号
     * @param status  状态
     * @return 统计数
     */
    @Override
    public Integer countByEvent(String eventId, Integer status) {
        HashMap<String, Object> parameter = Maps.newHashMap();
        parameter.put("eventId", eventId);
        parameter.put("isSuccess", status);
        return projectReadDao.count(parameter);
    }

    @Override
    public Double sumForActualAmount(ProjectAnalyze projectAnalyze, Integer amountType) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("targetId", projectAnalyze.getTargetId());
        map.put("eventId", projectAnalyze.getEventId());
        map.put("payType", projectAnalyze.getPayType());
        map.put("amountType", amountType);
        Double result = projectReadDao.sumForActualAmount(map);
        return null == result ? 0d : result;
    }


    /**
     * @Description:获取项目或者活动的总金额
     * @Param: [projectAnalyze, amountType]
     * @return: java.lang.Float
     * @Author: Hua Qunhao
     * @Date: 2019-02-12-0012
     */
    @Override
    public Double sumProjectOREventAmount(ProjectAnalyze projectAnalyze, Integer amountType) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("targetId", projectAnalyze.getTargetId());
        map.put("eventId", projectAnalyze.getEventId());
        map.put("payType", projectAnalyze.getPayType());
        map.put("amountType", amountType);
        map.put("isSuccess", projectAnalyze.getIsSuccess());
        map.put("activityCreateBy", projectAnalyze.getActivityCreateBy());
        Double fAmount = projectReadDao.sumProjectOREventAmount(map);
        return null == fAmount ? 0d : fAmount;
    }


    @Override
    public List<Project> getInitUseTimeData() {
        return projectReadDao.getInitUseTimeData();
    }

    @Override
    public Integer countByDate(String memberId, String startDate, String endDate) {
        HashMap<String, Object> parameter = Maps.newHashMap();
        parameter.put("startDate", startDate);
        parameter.put("endDate", endDate);
        parameter.put("memberId", memberId);
        Integer count = projectReadDao.countByDate(parameter);
        return null == count ? 0 : count;
    }


    /**
     * 统计众筹成功数
     *
     * @param memberId 发布者
     * @return 成功数
     */
    @Override
    public Integer count(String memberId) {
        return this.countByDate(memberId, null, null);
    }


    /**
     * 统计众筹项目下的浏览量
     *
     * @param targetId 众筹项目编号
     * @return 浏览量
     */
    @Override
    public Integer viewNumForTargetId(String targetId) {
        return null;
    }

    /**
     * 根据用户和项目id获取众筹
     *
     * @param targetId 项目id
     * @param authorId 用户id
     * @return
     */
    @Override
    public Project findByTargetWithAuthor(String targetId, String authorId) {
        return projectReadDao.findByTargetWithAuthor(targetId, authorId);
    }

    /**
     * 查询某机构下的所有众筹名者
     *
     * @param partnerId
     * @return
     */
    public List<Member> getZcMembersByPartnerId(String partnerId) {
        return projectReadDao.getZcMembersByPartnerId(partnerId);
    }

    /**
     * 按分组查到某机构某会员的众筹数和众筹金额(众筹中和众筹成功的)
     *
     * @return
     */
    public List<Map<String, Object>> getAllGroup(Integer status, String partnerId, String memberId) {
        return projectReadDao.getAllGroup(status, partnerId, memberId);
    }

    @Override
    public List<Map<String, Object>> getAllGroupAPI(Integer status, String partnerId, String memberId) {
        return getAllGroup(status, partnerId, memberId);
    }

    @Override
    public Map<String, Object> countAnalyzeByDate(String startDate, String endDate, Map<String, Object> params) {
        return projectReadDao.countAnalyzeByDate(startDate, endDate, params);
    }

    /**
     * 根据当前项目id和目标编号获取已报名且众筹状态正确的众筹
     *
     * @param targetId 目标编号
     * @param sourceId 当前项目id
     * @return 众筹列表
     */
    public List<ProjectBatchTransfer> findByTargetWithSource(String targetId, String sourceId) {
        return projectReadDao.findByTargetWithSource(targetId, sourceId);
    }

    /**
     * 根据当前项目id和目标编号获取未报名的
     *
     * @param targetId 目标编号
     * @param sourceId 当前项目id
     * @return 众筹列表
     */
    public List<Project> findByTargetWithSourceNotInvolved(String targetId, String sourceId) {
        return projectReadDao.findByTargetWithSourceNotInvolved(targetId, sourceId);
    }

    @Override
    public ProjectDetailCountOutput getDataTable(ProjectDetailCountInput input) {
        return projectReadDao.getDataTable(input);
    }

    @Override
    public Integer countBySuccessDate(Date successDate, String partnerId) {
        return projectReadDao.countBySuccessDate(successDate, partnerId);
    }

    @Override
    public List<String> getPartnerIdByAuthorId(String authorId) {
        return projectReadDao.getPartnerIdByAuthorId(authorId);
    }

    @Override
    public List<ProjectWithAuthor> listTransferWithAuthorPage(ProjectWithAuthor projectWithAuthor, Page page) {
        return projectReadDao.listTransferWithAuthorPage(projectWithAuthor, page);
    }

    @Override
    public ProjectWithAuthor getProjectAuthorByOrder(String orderId) {
        return projectReadDao.getProjectAuthorByOrder(orderId);
    }

    @Override
    public Integer disCount(String memberId, String startDate, String endDate) {
        HashMap<String, Object> parameter = Maps.newHashMap();
        parameter.put("startDate", startDate);
        parameter.put("endDate", endDate);
        parameter.put("memberId", memberId);
        Integer count = projectReadDao.disCount(parameter);
        return null == count ? 0 : count;
    }

    @Override
    public List<String> findByOpenId(String openId) {
        return projectReadDao.findByOpenId(openId);
    }

    @Override
    public Project getMyProject(String targetId, String authorId) {
        return projectReadDao.getMyProject(targetId, authorId);
    }

    @Override
    public List<Project> inits() {
        return projectReadDao.inits();
    }

    /***
     * 根据项目id找该项目下所有的报名者
     * @param targetId
     * @return
     */
    @Override
    public List<Project> getProjectByActivityId(String targetId, Page page) {
        return projectReadDao.getProjectByActivityId(targetId, page);
    }

    /***
     * 根据项目id 获取主题下面所有的报名人员
     * @param eventId
     * @param page
     * @return
     */
    @Override
    public List<Project> queryProjectByTargetId(String eventId, Page page) {
        return projectReadDao.queryProjectByTargetId(eventId, page);
    }

    /***
     * 个人排行榜
     * @param targetId
     * @param page
     * @return
     */
    @Override
    public List<Project> popularityList(String targetId, Page page, String type) {
        return projectReadDao.popularityList(targetId, page, type);
    }

    /***
     * 个人排行榜  自己的个人信息
     * @param targetId
     * @param
     * @return
     */
    @Override
    public Project popularity(String targetId, String type, String memberId) {
        return projectReadDao.popularity(targetId, memberId, type);
    }

    @Override
    public Project getByMemberId(String targetId, String memberId) {
        return projectReadDao.getByMemberId(targetId, memberId);
    }

    /***
     * 根据项目id和supportId 找报名者
     * @param supportId
     * @param targetId
     * @return
     */
    @Override
    public Project getProjectByMemberId(String supportId, String targetId) {
        return projectReadDao.getProjectByMemberId(supportId, targetId);
    }

    @Override
    public List<ProjectWithAuthor> listForMySupport(ProjectWithAuthor projectWithAuthor, Page page) {
        return projectReadDao.listForMySupport(projectWithAuthor, page);
    }

    @Override
    public void updateProjectTitle(String actId) {
        projectWriteDao.updateProjectTitle(actId);
    }

    /***
     * 众筹者转移初始化LastSupportDate
     * @return
     */
    @Override
    public List<Project> transferLastSupportDate() {
        return projectReadDao.transferLastSupportDate();
    }

    @Override
    public List<ProjectWithAuthorOutput> listForActGradation(ProjectWithAuthor projectWithAuthor, Map<String, Object> params, Page page) {
        return projectReadDao.listForActGradation(projectWithAuthor, params, page);
    }

    @Override
    public List<ProjectWithAuthor> batchList(HashMap<String, Object> params, Page page) {
        return projectReadDao.list(params, page);
    }

    /**
     * 联合查询创建者__多场
     *
     * @param projectWithAuthor 查询参数
     * @param page              分页参数
     * @return 众筹列表
     */
    @Override
    public List<ProjectWithAuthor> gradationListWithAuthorPage(ProjectWithAuthor projectWithAuthor, Page page) {
        return projectReadDao.gradationListWithAuthorPage(projectWithAuthor, page);
    }

    /**
     * @Description: 根据众筹者报名的众筹类型（单/多场）来选择替换
     * @Param: project
     * @return: project
     * @Author: Hua Qunhao
     * @Date: 2019-03-28-0028
     */
    @Override
    public Project isGradationReplace(Project project, EventGradation ongoingGradation) throws Exception {
        char[] crr = new char[]{'零', '一', '二', '三', '四', '五'};
        double price = ongoingGradation.getPrice();
        project.setTargetAmount((float) price);
        project.setGradationName("第" + crr[ongoingGradation.getGradation()] + "场");
        return project;
    }

    @Override
    public void updateIsSuccess(Integer isSuccess, String projectId) {
        projectWriteDao.updateIsSuccess(isSuccess, projectId);
    }

    /**
    * @Author hzq
    * @Description //找到这个活动下面的所有报名人员
    * @Date 17:58 2019/4/10
    * @param eventId
    * @return
    **/
    @Override
    public List<Project> findByEventId(String eventId) {
        return projectReadDao.findByEventId(eventId);
    }

    @Override
    public Project getSupportMoneyAndCount(String projectId) {
        return projectReadDao.getSupportMoneyAndCount(projectId);
    }

    /**
     * 统计众筹项目下的浏览量
     * @param eventId 目标编号
     * @return 浏览量
     */
    @Override
    public Integer viewNumForEventId(String eventId) {
        return projectReadDao.viewNumForEventId(eventId);
    }


    /**
     * @Description: 判断众筹者是否可以退款
     * @Param:  projectId
     * @return: isCan
     * @Author: Hua Qunhao
     * @Date: 2019-04-29-0029
     */
    public boolean isCanRefund (String projectId) {

        boolean isCan = true;
        Project project = projectReadDao.get(projectId);

        //如果状态为众筹成功，不能退款
        if(Constant.IS_CROWFUND_SUCCESS==project.getIsSuccess() ){
            isCan = false;
        }
        return isCan;
    }

    @Override
    public List<CrowdfundingBattleReport> getProjectBattleReport(Map<String, Object> param, Integer type) {
        return projectReadDao.getProjectBattleReport(param,type);
    }
}
